using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Microblaze.Simulator.Engine;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Branch Immediate if Less Than
    /// </summary>
    public class Blti
      : ConditionalBranchInstruction
    {
        private enum BltiInstructionKind
        {
            None,   // error
            blti,   // branch immediate if Less Than
            bltid   // branch immediate if Less Than with Delay
        }

        public override int CycleCount
        {
            get
            {
                if (BranchStatus == BranchInstructionCycleCountStatus.BranchTaken)
                    return 1;
                else
                    return kind == BltiInstructionKind.bltid ? 2 : 3;
            }
        }

        private BltiInstructionKind kind;

        public Blti()
            : base("blti")
        {
        }

        public override void Set(uint address, uint value)
        {
            base.Set(address, value);

            kind =
              (DestinationRegister == 0x02) ? BltiInstructionKind.blti :
              (DestinationRegister == 0x12) ? BltiInstructionKind.bltid :
              BltiInstructionKind.None;

            if (kind == BltiInstructionKind.None)
                throw new UnknownInstructionException(value);
        }

        /// <summary>
        /// Branch if rA is less than 0, to the instruction located in the offset value of 
        /// IMM. The targetof the branch will be the instruction at address PC + IMM.
        /// The mnemonic bltid will set theDbit. The D bit determines whether there is a 
        /// branch delay slot or not. If the D bit is set, it means that there is a delay 
        /// slot and the instruction following the branch (i.e. in the branch delay slot) 
        /// is allowed to complete execution before executing the target instruction. If 
        /// the D bit is not set, it means that there is no delay slot, so the instruction 
        /// to be executed after the branch is the target instruction.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            // note use of int for comparison
            int sa = (int)MicroblazeComponent.ReadRegister(SourceRegisterA);

            if (sa < 0)
            {
                MicroblazeComponent.Pc = MicroblazeComponent.Pc + GetImmediateValue(MicroblazeComponent);
                BranchStatus = BranchInstructionCycleCountStatus.BranchTaken;

                if (kind == BltiInstructionKind.bltid)
                    MicroblazeComponent.DelayedBranchActive = true;
            }
            else
            {
                MicroblazeComponent.Pc += 4;
                BranchStatus = BranchInstructionCycleCountStatus.BranchNotTaken;
            }
        }

        public override string ToString()
        {
            return string.Format("{0} r{1}, {2}",
              kind,
              SourceRegisterA,
              ImmediateValue
            );
        }
    }
}
